package com.imufe.chemicalenterprisedangerwarning.util;

import com.imufe.chemicalenterprisedangerwarning.exception.ValidationException;

import java.lang.reflect.InvocationTargetException;

/**
 * @author BaiBan
 * @since 2022/9/29
 */
public class BeanUtils {
    /**
     * POJO 之间的转换
     */
    public static <E, T> T copyPOJO(E e, Class<T> clazz) {
        try {
            T t = clazz.getDeclaredConstructor().newInstance();
            if (e == null) return t;
            org.springframework.beans.BeanUtils.copyProperties(e, t);
            return t;
        } catch (InstantiationException | InvocationTargetException | IllegalAccessException |
                 NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }

    private final static String chineseValue = "零一二三四五六七八九";

    /**
     * 阿拉伯数字转大写
     * 假定不超过1000条
     */
    public static String digitalToChinese(Integer digital) throws ValidationException {
        if(digital <= 0 || digital >= 100)
            throw new ValidationException("条例越界，不能大于等于100且小于等于0");

        StringBuilder builder = new StringBuilder();

        // 十位
        if (digital >= 10 && digital / 10 != 1)
            builder.append(chineseValue.charAt(digital / 10));
        if(digital >= 10)
            builder.append("十");
        // 个位
        if (digital % 10 != 0)
            builder.append(chineseValue.charAt(digital % 10));

        return String.format("第%s条", builder);
    }
}
